package com.lanju.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanju.common.SpringUtils;
import com.lanju.entity.User;
import com.lanju.service.JedisService;
import com.lanju.service.YezhuCaseService;

@Controller
@RequestMapping("wx_yezhucase")
public class YezhuCaseController {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(YezhuCaseController.class);
	
	@Autowired
	private YezhuCaseService yezhuService;
	
	 @Resource
		JedisService jedisService;
	
	@RequestMapping("addIntroduce")
	@ResponseBody
	public Map<String,Object> addIntroduce(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		param.put("userCode", user.getUserId());
		return yezhuService.addIntroduce(param);
	}
	@RequestMapping("updateIntroduce")
	@ResponseBody
	public Map<String,Object> updateIntroduce(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		param.put("userCode", user.getUserId());
		return yezhuService.updateIntroduce(param);
	}
	@RequestMapping("getIntroduceDetail")
	@ResponseBody
	public Map<String,Object> getIntroduceDetail(HttpServletRequest request){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return yezhuService.getIntroduceDetail(param);
	}
	
	@RequestMapping("getAllCaseList")
	@ResponseBody
	public Map<String,Object> getAllCaseList(HttpServletRequest request){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return yezhuService.getAllCaseList(param);
	}
	/**
	 * 获取保洁卡余额
	 * @return
	 */
	@RequestMapping("getCleanCard")
	@ResponseBody
	public Map<String,Object> getCleanCard(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		if(user ==null ){
		 Map<String,Object> result=new HashMap<String, Object>();
		 result.put("money", null);
		return result;
		}
		return yezhuService.getCleanCard(user.getPhone());
		
	}
	
	/**
	 * 上传优势
	 * @param files
	 * @param advTitle
	 * @param advDesc
	 * @return
	 */
    @RequestMapping("filesUpload")
	@ResponseBody
	public String filesUpload(
			@RequestParam(value="files",required=false) MultipartFile[] files,
			@RequestParam(value="advTitle") String advTitle,
			@RequestParam(value="advDesc",required=false) String advDesc) {
		
		String fileUrl="";
		String fileTypeStr="";
		String result="success";
		// 判断file数组不能为空并且长度大于0
		if (files != null && files.length > 0) {
			// 循环获取file数组中得文件
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];
				try {
					
					 OutputStream os = null;
					 InputStream in= null;
					 String picPath=null;
					in=file.getInputStream();
					 // 2、保存到临时文件
		            // 1K的数据缓冲
		            byte[] bs = new byte[1024];
		            // 读取到的数据长度
		            int len;
		            // 输出的文件流保存到本地文件
		            File tempFile = new File("/webapp/lanjufile/case");//linux
//		            File tempFile = new File("D:/upFiles/case");
		            if (!tempFile.exists()) {
		                tempFile.mkdirs();
		            }else{
		            	LOGGER.info("文件已存在");
		            }
//		            String fullPath="D:/upFiles/case/"+file.getOriginalFilename();
		            String fullPath="/webapp/lanjufile/case/"+file.getOriginalFilename();//linux
		            String vmURL="/dataResourceImages/case/"+file.getOriginalFilename();
		            System.out.println("文件全路径"+fullPath);
		            System.out.println("vmURL=="+vmURL);
		            fileUrl+=vmURL+",";
		            String[] fullPathArr=fullPath.split("\\.");
		            fileTypeStr+=fullPathArr[fullPathArr.length-1]+",";
		            os = new FileOutputStream(fullPath);
		            // 开始读取
		            while ((len = in.read(bs)) != -1) {
		                os.write(bs, 0, len);
		            } 
		            os.close();
	                in.close();
	                System.out.println("文件保存的路径是："+fileUrl);
	             // 保存文件

				} catch (IOException e) {
					 result="fail";
					e.printStackTrace();
				}
				
			}
			
		}else{
			 
			
		}
		// 重定向
		return result;
	}
    
}
